from flask import request
from SQLOperation.UserSQLOperation import UserSQLOperation
from SQLConfig import app


@app.route('/updateUserInformation', methods=['Get', 'POST'])
def updateUserInformation():  # put application's code here
    """
    修改用户信息
    """

    # 判断是否存在此用户
    username = request.json.get("username")
    user = UserSQLOperation.searchByUsername(username)
    if user is None:
        return "user not exist"

    # 修改密码
    oldPassword = request.json.get("oldPassword")
    newPassword = request.json.get("newPassword")
    if oldPassword is not None and newPassword is not None:
        if user.password == oldPassword:
            UserSQLOperation.updateByUsername(username=username, password=newPassword)
        else:
            return "password wrong"

    # 修改手机号
    phone = request.json.get("phone")
    if phone is not None:
        UserSQLOperation.updateByUsername(username=username, phone=phone)

    # 修改邮箱
    email = request.json.get("email")
    if email is not None:
        UserSQLOperation.updateByUsername(username=username, email=email)

    # 修改姓名
    name = request.json.get("name")
    if name is not None:
        UserSQLOperation.updateByUsername(username=username, name=name)

    # 修改性别
    gender = request.json.get("gender")
    if gender is not None:
        UserSQLOperation.updateByUsername(username=username, gender=gender)

    # 修改地区
    region = request.json.get("region")
    if region is not None:
        UserSQLOperation.updateByUsername(username=username, region=region)

    # 修改自我介绍
    introduction = request.json.get("introduction")
    if introduction is not None:
        UserSQLOperation.updateByUsername(username=username, introduction=introduction)

    return "OK"
